<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>王者荣耀英雄列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            color: #333;
        }

       .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        h1 {
            text-align: center;
            margin-bottom: 20px;
            color: #d4a76a;
        }

       .filter-buttons {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }

       .filter-btn {
            padding: 8px 15px;
            margin: 0 5px 10px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }

       .filter-btn:hover,.filter-btn.active {
            background-color: #d4a76a;
            color: #fff;
            border-color: #d4a76a;
        }

       .hero-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
            gap: 15px;
        }

       .hero-item {
            background-color: #fff;
            border-radius: 5px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s;
            text-align: center;
        }

       .hero-item:hover {
            transform: translateY(-5px);
        }

       .hero-img {
            width: 100%;
            height: auto;
            display: block;
        }

       .hero-name {
            padding: 8px;
            font-size: 14px;
            color: #333;
        }

       .hero-occupation {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }

       .loading {
            text-align: center;
            padding: 20px;
            font-size: 16px;
            color: #666;
        }

       .search-container {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }

       .search-input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 300px;
            margin-right: 10px;
        }

       .search-button {
            padding: 8px 15px;
            background-color: #d4a76a;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }

       .search-button:hover {
            background-color: #b98e53;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>王者荣耀英雄列表</h1>
        <div class="search-container">
            <input type="text" class="search-input" id="searchInput" placeholder="输入英雄名称搜索">
            <button class="search-button" id="searchButton">搜索</button>
        </div>
        <div class="filter-buttons">
            <button class="filter-btn active" data-occupation="all">全部</button>
            <button class="filter-btn" data-occupation="战士">战士</button>
            <button class="filter-btn" data-occupation="法师">法师</button>
            <button class="filter-btn" data-occupation="坦克">坦克</button>
            <button class="filter-btn" data-occupation="刺客">刺客</button>
            <button class="filter-btn" data-occupation="射手">射手</button>
            <button class="filter-btn" data-occupation="辅助">辅助</button>
        </div>
        <div class="hero-list" id="heroList">
            <div class="loading">加载中...</div>
        </div>
    </div>

    <script>
        async function fetchData(url) {
            try {
                const response = await fetch(url);
                const data = await response.text();

                const jsonData = data.replace(/^[a-zA-Z]+/, '').replace(/^\(/, '').replace(/\)$/, '');

                const parsedData = JSON.parse(jsonData);

                const filteredObj = parsedData.data.map(item => ({
                    title: item.title,
                    occupation: item.occupation,
                    heroImg: item.heroimg
                }));
                const res = {
                    data: filteredObj.slice(1)
                }
                return res
            } catch (error) {
                console.error('请求出错:', error);
                return []
            }
        }

        function renderHeroList(heroes, occupation = 'all', searchTerm = '') {
            const heroListElement = document.getElementById('heroList');

            let filteredHeroes = occupation === 'all'
               ? heroes
                : heroes.filter(hero => hero.occupation === occupation);

            if (searchTerm) {
                filteredHeroes = filteredHeroes.filter(hero =>
                    hero.title.replace('故事站-英雄列表-', '').includes(searchTerm)
                );
            }

            if (filteredHeroes.length === 0) {
                heroListElement.innerHTML = '<div class="loading">暂无符合条件的英雄数据</div>';
                return;
            }

            const html = filteredHeroes.map(hero => `
                <div class="hero-item">
                    <img src="${hero.heroImg}" alt="${hero.title}" class="hero-img">
                    <div class="hero-name">${hero.title.replace('故事站-英雄列表-', '')}</div>
                    <div class="hero-occupation">${hero.occupation}</div>
                </div>
            `).join('');

            heroListElement.innerHTML = html;
        }

        async function init() {
            const url = 'https://pvp.qq.com/webplat/info/news_version3/15592/18024/23901/24397/24398/m17330/list_1.shtml?callback=createHeroList';
            const res = await fetchData(url);

            if (res.data && res.data.length > 0) {
                renderHeroList(res.data);

                const filterButtons = document.querySelectorAll('.filter-btn');
                filterButtons.forEach(button => {
                    button.addEventListener('click', () => {
                        filterButtons.forEach(btn => btn.classList.remove('active'));
                        button.classList.add('active');

                        const occupation = button.dataset.occupation;
                        const searchTerm = document.getElementById('searchInput').value;

                        renderHeroList(res.data, occupation, searchTerm);
                    });
                });

                const searchButton = document.getElementById('searchButton');
                searchButton.addEventListener('click', () => {
                    const searchTerm = document.getElementById('searchInput').value;
                    const activeButton = document.querySelector('.filter-btn.active');
                    const occupation = activeButton.dataset.occupation;

                    renderHeroList(res.data, occupation, searchTerm);
                });
            } else {
                document.getElementById('heroList').innerHTML = '<div class="loading">数据加载失败，请刷新重试</div>';
            }
        }

        document.addEventListener('DOMContentLoaded', init);
    </script>
</body>

</html>
    